﻿Imports System.Data.OleDb

Module PermissionMenu
    Public Function PhanQuyenForm(ByVal MaForm As String, ByVal UserId As Integer) As Boolean()
        Try
            Dim dtQuyen As New DataTable
            Dim query As String = "Select Xem,ThemMoi,ChinhSua,Xoa,Duyet,InData From PhanQuyen" & _
                                  " Where GroupId=(Select GroupId From Users Where UserId=" & UserId & ") And IDChucNang='" & MaForm & "'"
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim adapter As New OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtQuyen)
            dbConn.Close()
            Dim aQuyen(5) As Boolean
            aQuyen(0) = IIf(dtQuyen.Rows(0)("ThemMoi") = "1", True, False)
            aQuyen(1) = IIf(dtQuyen.Rows(0)("ChinhSua") = "1", True, False)
            aQuyen(2) = IIf(dtQuyen.Rows(0)("Xoa") = "1", True, False)
            aQuyen(3) = IIf(dtQuyen.Rows(0)("Duyet") = "1", True, False)
            aQuyen(4) = IIf(dtQuyen.Rows(0)("InData") = "1", True, False)
            Return aQuyen
        Catch ex As Exception
            Throw
        End Try
    End Function
    Public Sub PhanQuyenMenu(ByVal userid As Integer)
        Try
            Dim dtMenu As New DataTable
            Dim query As String = "Select IDChucNang,Xem,ThemMoi,ChinhSua,Xoa,InData From PhanQuyen" & _
                                  " Where GroupId=(Select GroupId From Users Where UserId=" & userid & ")"
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim adapter As New OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtMenu)
            dbConn.Close()
            For i As Integer = 0 To dtMenu.Rows.Count - 1
                'Any
                frmMain.MenuDangXuat.Enabled = True
                frmMain.MenuKetThuc.Enabled = True
                frmMain.MenuDoiMatKhau.Enabled = True
                'Group Admin
                If dtMenu.Rows(i)("IDChucNang") = "MenuNguoiSuDung" Then
                    'Xem
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuNguoiSuDung.Enabled = True
                    Else
                        frmMain.MenuNguoiSuDung.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuNhomSuDung" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuNhomSuDung.Enabled = True
                    Else
                        frmMain.MenuNhomSuDung.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuPhanQuyen" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuPhanQuyen.Enabled = True
                    Else
                        frmMain.MenuPhanQuyen.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuIn" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuPrinter.Enabled = True
                    Else
                        frmMain.MenuPrinter.Enabled = False
                    End If
                    Continue For
                End If
                
                'Group Sale
                If dtMenu.Rows(i)("IDChucNang") = "MenuBangGiaSanPham" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuBangGiaSanPham.Enabled = True
                    Else
                        frmMain.MenuBangGiaSanPham.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuBanHang" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuBanHang.Enabled = True
                    Else
                        frmMain.MenuBanHang.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuKhachHang" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuKhachHang.Enabled = True
                    Else
                        frmMain.MenuKhachHang.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuKhuVuc" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuKhuVuc.Enabled = True
                    Else
                        frmMain.MenuKhuVuc.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuMucTruHoaDon" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuMucTruHoaDon.Enabled = True
                    Else
                        frmMain.MenuMucTruHoaDon.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuSanPhamNhieuGia" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuSanPhamNhieuGia.Enabled = True
                    Else
                        frmMain.MenuSanPhamNhieuGia.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuGiamTruRieng" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuGiamTruRieng.Enabled = True
                    Else
                        frmMain.MenuGiamTruRieng.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuGiamTruChung" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuMucTruHoaDon.Enabled = True
                    Else
                        frmMain.MenuMucTruHoaDon.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuNhanVienTiepThi" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuNhanVienTiepThi.Enabled = True
                    Else
                        frmMain.MenuNhanVienTiepThi.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuNhomGiaSanPham" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuNhomGia.Enabled = True
                    Else
                        frmMain.MenuNhomGia.Enabled = False
                    End If
                    Continue For
                End If
                'Group Accountant
                If dtMenu.Rows(i)("IDChucNang") = "MenuThongKeNhapKho" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuThongKeNhapKho.Enabled = True
                    Else
                        frmMain.MenuThongKeNhapKho.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuThongKeXuatKho" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuThongKeXuatKho.Enabled = True
                    Else
                        frmMain.MenuThongKeXuatKho.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuThongKeSoLuongTon" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuThongKeSoLuongTon.Enabled = True
                    Else
                        frmMain.MenuThongKeSoLuongTon.Enabled = False
                    End If
                    Continue For
                End If

                'Group WareHouse Management
                If dtMenu.Rows(i)("IDChucNang") = "MenuDonViTinh" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuDonViTinh.Enabled = True
                    Else
                        frmMain.MenuDonViTinh.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuHangTraVe" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuHangTraVe.Enabled = True
                    Else
                        frmMain.MenuHangTraVe.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuKhoThanhPham" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuKho.Enabled = True
                    Else
                        frmMain.MenuKho.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuLoaiSanPham" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuLoaiSanPham.Enabled = True
                    Else
                        frmMain.MenuLoaiSanPham.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuSanPham" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuSanPham.Enabled = True
                    Else
                        frmMain.MenuSanPham.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuNhapKho" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuNhapKho.Enabled = True
                    Else
                        frmMain.MenuNhapKho.Enabled = False
                    End If
                    Continue For
                End If

                If dtMenu.Rows(i)("IDChucNang") = "MenuPhieuXuatKho" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuXuatKhoNoiBo.Enabled = True
                    Else
                        frmMain.MenuXuatKhoNoiBo.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuKhoXuLy" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuKhoXuLy.Enabled = True
                    Else
                        frmMain.MenuKhoXuLy.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuTheoDoiTon" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuTheoDoiTonKho.Enabled = True
                    Else
                        frmMain.MenuTheoDoiTonKho.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuTheoDoiDonHang" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuTheoDoiDonHang.Enabled = True
                    Else
                        frmMain.MenuTheoDoiDonHang.Enabled = False
                    End If
                    Continue For
                End If
                'Groupt Management MenuTheoDoiTon
                If dtMenu.Rows(i)("IDChucNang") = "MenuDoanhSoKhachHang" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuDoanhSoKhachHang.Enabled = True
                    Else
                        frmMain.MenuDoanhSoKhachHang.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuSanLuongTieuThu" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuSanLuongTieuThu.Enabled = True
                    Else
                        frmMain.MenuSanLuongTieuThu.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuThongKeNhapKho1" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuThongKeNhapKho1.Enabled = True
                    Else
                        frmMain.MenuThongKeNhapKho1.Enabled = False
                    End If
                    Continue For
                End If

                If dtMenu.Rows(i)("IDChucNang") = "MenuThongKeSoLuongTon1" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuThongKeSoLuongTon1.Enabled = True
                    Else
                        frmMain.MenuThongKeSoLuongTon1.Enabled = False
                    End If
                    Continue For
                End If

                If dtMenu.Rows(i)("IDChucNang") = "MenuThongKeXuatKho1" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuThongKeXuatKho1.Enabled = True
                    Else
                        frmMain.MenuThongKeXuatKho1.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuPhiVanChuyen" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuPhiVanChuyen.Enabled = True
                    Else
                        frmMain.MenuPhiVanChuyen.Enabled = False
                    End If
                    Continue For
                End If
                If dtMenu.Rows(i)("IDChucNang") = "MenuThongKeXuatKhoNoiBo1" Then
                    If dtMenu.Rows(i)("Xem") = "1" Then
                        frmMain.MenuThongKeXuatKhoNoiBo.Enabled = True
                    Else
                        frmMain.MenuThongKeXuatKhoNoiBo.Enabled = False
                    End If
                    Continue For
                End If
                '
            Next
        Catch ex As Exception

        End Try
    End Sub
    Public Sub AuthenticationForm(ByVal Approved As Boolean)
        Try
            If Approved Then
                'Kiểm Tra Quyền
                'Dim dataAccess As New DataAccess
                'Dim dbConn As New OleDb.OleDbConnection
                'Dim dbCommand As OleDb.OleDbCommand
                'Dim query As String = "Se"
                'dataAccess.ConnectDatabase_DBAccess(dbConn)
                'dbCommand = New OleDbCommand(query, dbConn)
                'If dbCommand.ExecuteScalar() = 0 Then
                '    Panel_SanPham.Visible = False
                '    MessageBox.Show("Mã hàng bạn chọn chưa có trong danh mục sản phẩm.Vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                '    txtSanPham.Focus()
                '    EmptyData(txtSanPham)
                '    Exit Sub
                'End If
            Else
                MenuHeThong(False)
                MenuQuanLyBanHang(False)
                MenuQuanLyKho(False)
                MenuKeToan(False)
                MenuThongKeBaoCao(False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.InnerException.ToString)
        End Try
    End Sub
    Public Sub MenuHeThong(ByVal status As Boolean)
        Try
            frmMain.MenuKetThuc.Enabled = status
            frmMain.MenuDangXuat.Enabled = status
            frmMain.MenuDoiMatKhau.Enabled = status
            frmMain.MenuNhomSuDung.Enabled = status
            frmMain.MenuNguoiSuDung.Enabled = status
            frmMain.MenuPhanQuyen.Enabled = status
            frmMain.MenuSaoLuu.Enabled = status
            frmMain.MenuPhuchoi.Enabled = status
            frmMain.MenuPrinter.Enabled = status
        Catch ex As Exception

        End Try
    End Sub
    Public Sub MenuQuanLyBanHang(ByVal status As Boolean)
        Try
            frmMain.MenuKhuVuc.Enabled = status
            frmMain.MenuKhachHang.Enabled = status
            frmMain.MenuNhanVienTiepThi.Enabled = status
            frmMain.MenuBangGiaSanPham.Enabled = status
            frmMain.MenuGiamTruRieng.Enabled = status
            frmMain.MenuSanPhamNhieuGia.Enabled = status
            frmMain.MenuMucTruHoaDon.Enabled = status
            frmMain.MenuBanHang.Enabled = status
            frmMain.MenuNhomGia.Enabled = status
        Catch ex As Exception

        End Try
    End Sub
    Public Sub MenuQuanLyKho(ByVal status As Boolean)
        Try
            frmMain.MenuDonViTinh.Enabled = status
            frmMain.MenuLoaiSanPham.Enabled = status
            frmMain.MenuSanPham.Enabled = status
            frmMain.MenuNhapKho.Enabled = status
            frmMain.MenuHangTraVe.Enabled = status
            frmMain.MenuKho.Enabled = status
            frmMain.MenuXuatKhoNoiBo.Enabled = status
            frmMain.MenuKhoXuLy.Enabled = status
        Catch ex As Exception

        End Try
    End Sub
    Public Sub MenuKeToan(ByVal status As Boolean)
        Try
            frmMain.MenuThongKeNhapKho.Enabled = status
            frmMain.MenuThongKeXuatKho.Enabled = status
            frmMain.MenuThongKeSoLuongTon.Enabled = status
        Catch ex As Exception

        End Try
    End Sub
    Public Sub MenuThongKeBaoCao(ByVal status As Boolean)
        Try
            frmMain.MenuDoanhSoKhachHang.Enabled = status
            frmMain.MenuSanLuongTieuThu.Enabled = status
            frmMain.MenuThongKeNhapKho1.Enabled = status
            frmMain.MenuThongKeXuatKho1.Enabled = status
            frmMain.MenuThongKeSoLuongTon1.Enabled = status
            frmMain.MenuPhiVanChuyen.Enabled = status
        Catch ex As Exception

        End Try
    End Sub
End Module